﻿using CodeCreator;
using iBing.Common.DbHelper;
using iBing.Common.DbHelper.CoreObj;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CodeTemplate.Template
{
    public class ToolForGetSqls
    {
        private string tableFullName;
        private List<ObjJavaField> javaFields;
        private string sql;
        private List<DbField> dbFields;

        public ToolForGetSqls(string schemaName, string tableName, List<ObjJavaField> javaFields)
        {
            if (schemaName != null && schemaName != "")
                this.tableFullName = schemaName + "." + tableName;
            else
                this.tableFullName = tableName;
            this.javaFields = javaFields;
        }

        public ToolForGetSqls(string sql, List<DbField> dbFields)
        {
            this.sql = sql;
            this.dbFields = dbFields;
        }

        public List<ObjForSql> GetDataForTable()
        {
            List<ObjForSql> sqls = new List<ObjForSql>();
            sqls.Add(new ObjForSql("select", getSelectSql()));
            sqls.Add(new ObjForSql("insert", getInsertSql()));
            sqls.Add(new ObjForSql("update", getUpdateSql()));
            sqls.Add(new ObjForSql("delete", getDeleteSql()));
            return sqls;
        }

        public List<ObjForSql> GetDataForSql()
        {
            string strNewSql = "select ";
            foreach (DbField of in this.dbFields)
            {
                strNewSql += "\r\n\t\t" + of.FieldName + " as " + Tools.DbFile2JavaFieldWidtFirstLower(of.FieldName) + ",";
            }
            if (strNewSql.EndsWith(","))
                strNewSql = strNewSql.Substring(0, strNewSql.Length - 1);
            strNewSql += "\r\n\tfrom (";
            strNewSql += "\r\n\t" + this.sql;
            strNewSql += "\r\n\t)";
            ObjForSql os = new ObjForSql("select", strNewSql);
            List<ObjForSql> osList = new List<ObjForSql>();
            osList.Add(os);
            return osList;
        }

        #region 获取对象信息
        private string getSelectSql()
        {
            string result = "";
            if (javaFields != null)
            {
                result = "select ";
                foreach (ObjJavaField of in javaFields)
                {
                    result += "\r\n\t" + of.DbFieldName + " as " + of.FieldName + ",";
                }
                if (result.EndsWith(","))
                    result = result.Substring(0, result.Length - 1);
                result += " from " + tableFullName;
                result += " where 1=1 ";
            }
            return result;
        }

        private string getDeleteSql()
        {
            string result = "";
            if (javaFields != null)
            {
                result = "delete from ";
                result += this.tableFullName;
                result += " where 1=1 and \r\n\t";
                foreach (ObjJavaField of in javaFields)
                {
                    if (of.IsPk)
                    {
                        result += of.DbFieldName;
                    }
                }
            }
            return result;
        }

        private string getInsertSql()
        {
            string result = "";
            if (javaFields != null)
            {
                string strColumns = "";
                string strValues = "";
                foreach (ObjJavaField of in javaFields)
                {
                    strColumns += "\r\n\t" + of.DbFieldName + ",";
                    switch (of.DbFieldType)
                    {
                        case "NUMBER":
                            strValues += "\r\n\t#{" + of.FieldName + ",jdbcType=NUMERIC},";
                            break;
                        case "LONG":
                            strValues += "\r\n\t#{" + of.FieldName + ",jdbcType=NUMERIC},";
                            break;
                        case "INTEGER":
                            strValues += "\r\n\t#{" + of.FieldName + ",jdbcType=NUMERIC},";
                            break;
                        case "FLOAT":
                            strValues += "\r\n\t#{" + of.FieldName + ",jdbcType=NUMERIC},";
                            break;
                        case "TIMESTAMP":
                            strValues += "\r\n\t#{" + of.FieldName + ",jdbcType=DATE},";
                            break;
                        case "TIMESTAMP(6)":
                            strValues += "\r\n\t#{" + of.FieldName + ",jdbcType=DATE},";
                            break;
                        case "DATE":
                            strValues += "\r\n\t#{" + of.FieldName + ",jdbcType=DATE},";
                            break;
                        default:
                            strValues += "\r\n\t#{" + of.FieldName + ",jdbcType=VARCHAR},";
                            break;
                    }
                }
                if (strColumns.EndsWith(","))
                    strColumns = strColumns.Substring(0, strColumns.Length - 1);
                if (strValues.EndsWith(","))
                    strValues = strValues.Substring(0, strValues.Length - 1);

                result = "insert into ";
                result += tableFullName;
                result += " (" + strColumns + ") ";
                result += " values ";
                result += "(" + strValues + ")";
            }
            return result;
        }

        private string getUpdateSql()
        {
            string result = "";
            if (javaFields != null)
            {
                string strColumns = "";
                string strWhere = "";
                foreach (ObjJavaField of in javaFields)
                {
                    if (of.IsPk)
                    {
                        strWhere += "\r\n\t" + of.DbFieldName + "=";
                        strWhere += "#{" + of.FieldName + "} and";
                    }
                    else
                    {
                        strColumns += "\r\n\t" + of.DbFieldName + "=";
                        switch (of.DbFieldType)
                        {
                            case "NUMBER":
                                strColumns += "\r\n\t#{" + of.FieldName + ",jdbcType=NUMERIC},";
                                break;
                            case "LONG":
                                strColumns += "\r\n\t#{" + of.FieldName + ",jdbcType=NUMERIC},";
                                break;
                            case "INTEGER":
                                strColumns += "\r\n\t#{" + of.FieldName + ",jdbcType=NUMERIC},";
                                break;
                            case "FLOAT":
                                strColumns += "\r\n\t#{" + of.FieldName + ",jdbcType=NUMERIC},";
                                break;
                            case "TIMESTAMP":
                                strColumns += "\r\n\t#{" + of.FieldName + ",jdbcType=DATE},";
                                break;
                            case "TIMESTAMP(6)":
                                strColumns += "\r\n\t#{" + of.FieldName + ",jdbcType=DATE},";
                                break;
                            case "DATE":
                                strColumns += "\r\n\t#{" + of.FieldName + ",jdbcType=DATE},";
                                break;
                            default:
                                strColumns += "\r\n\t#{" + of.FieldName + ",jdbcType=VARCHAR},";
                                break;
                        }
                    }
                }
                if (strColumns.EndsWith(","))
                    strColumns = strColumns.Substring(0, strColumns.Length - 1);
                if (strWhere.EndsWith("and"))
                    strWhere = strWhere.Substring(0, strWhere.Length - 3);

                result = "update ";
                result += this.tableFullName;
                result += " set " + strColumns + " ";
                result += " where " + strWhere;
            }
            return result;
        }

        private string objectToString(object obj)
        {
            if (obj != null)
                return obj.ToString();
            else
                return "";
        }
        #endregion
    }
}
